﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZGRemote.Common.Message;
using ZGRemote.Common.Message.RemoteFileExplorer;
using ZGRemote.Common.Processor;

namespace ZGRemote.Client.Handler
{
    [CanProcessMessage(typeof(GetFiles),
                       typeof(GetDrives))]
    public class RemoteFileExplorerHandler : HandlerBase<RemoteFileExplorerHandler>
    {
        public override void Excute(MessageBase message)
        {
            switch(message)
            {
                case GetFiles getFiles:
                    GetFiles(getFiles); 
                    break;

                case GetDrives getDrives:
                    GetDrives(getDrives); 
                    break;
            }
        }

        private void GetFiles(GetFiles message)
        {
            GetFilesResponse response = new GetFilesResponse();
            response.ID = message.ID;
            try
            {
                if (string.IsNullOrEmpty(message.Path))
                {
                    throw new Exception("File not found!");
                }

                DirectoryInfo pathInfo = new DirectoryInfo(message.Path);

                DirectoryInfo[] dirs = pathInfo.GetDirectories();
                FileInfo[] files = pathInfo.GetFiles();

                int size = dirs.Length + files.Length;
                response.FileName = new string[size];
                response.LastWriteTime = new DateTime[size];
                response.FileType = new byte[size];
                response.Size = new long[size];

                int i = 0;
                for(; i < dirs.Length; i++)
                {
                    response.FileName[i] = dirs[i].Name;
                    response.LastWriteTime[i] = dirs[i].LastWriteTime;
                    response.FileType[i] = 1;
                }

                for(; i < size; i++)
                {
                    response.FileName[i] = files[i - dirs.Length].Name;
                    response.LastWriteTime[i] = files[i - dirs.Length].LastWriteTime;
                    response.FileType[i] = 0;
                    response.Size[i] = files[i - dirs.Length].Length;
                }

                response.Status = true;
            }
            catch (Exception ex)
            {
                response.Status = false;
                response.Message = ex.Message;
            }
            SendMessage(UserContext, response);
        }

        private void GetDrives(GetDrives message)
        { 
            GetDrivesResponse response = new GetDrivesResponse();
            response.ID = message.ID;
            try
            {
                response.Names = DriveInfo.GetDrives().Select(d => d.Name).ToArray();
                response.Status = true;
            } 
            catch (Exception ex) 
            {
                response.Status = false;
                response.Message = ex.Message;
            }
            SendMessage(UserContext, response);
        }

    }
}
